-
Notifications
You must be signed in to change notification settings - Fork 948
Added support for DynamoDbAutoGeneratedKey annotation #6373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for DynamoDbAutoGeneratedKey annotation #6373
Conversation
519acfe to
fa35bfc
Compare
586dbf5 to
1c0b19f
Compare
335e532 to
f3a3ad1
Compare
d407b73 to
e2b74e3
Compare
fb82197 to
ede74c8
Compare
@marcusvoltolim
marcusvoltolim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests with @DynamoDbSortKey
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Outdated
Show resolved
Hide resolved
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Outdated
Show resolved
Hide resolved
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Show resolved
Hide resolved
...main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedKeyExtension.java
Outdated
Show resolved
Hide resolved
This solution still doesn't work for PartitionKey or SortKey, because UpdateBehavior are not applied to either as shown in the print below.
This is a workaround because it doesn't generate new values in primaryKeys because the new extension validates if the value is missing, but it doesn't solve the root problem. Therefore, each new extension that can be applied to a PK must address this issue. The best approach is to not pass PKs to WriteModification.
imageHello @marcusvoltolim,
Thank you very much for your review.
I have added tests for @DynamoDbSortKey as you suggested. The test class (AutoGeneratedKeyRecordTest) now covers all 4 supported key types with @DynamoDbAutoGeneratedKey:
- Primary partition key (
@DynamoDbPartitionKey) - Primary sort key (
@DynamoDbSortKey) - GSI partition key (
@DynamoDbSecondaryPartitionKey) - GSI sort key (
@DynamoDbSecondarySortKey)
Regarding your comment about @DynamoDbUpdateBehavior: You're absolutely correct. Based on the implementation, the @DynamoDbAutoGeneratedKey annotation works with @DynamoDbUpdateBehavior only for secondary index keys (GSI/LSI partition and sort keys). For primary keys (both partition and sort), UpdateBehavior has no effect since primary keys cannot be null in DynamoDB and are always required for update operations.
I have updated the tests, ticket description, and PR description to clearly reflect this.
Could you please take another look when you have a chance? Thank you!
Hi @anasatirbasa ,
I read through the PR, I have a few concerns I wanted to raise with the entire team before providing feedback. Will update you soon.
Hi @anasatirbasa, thanks for the wait.
I have reviewed this PR with the team and have a few points that we would ideally like to fix.
I agree that introducing this separate annotation would solve the problem in an "opt in" non invasive way.
Considerations:
- I'm trying to verify is whether the following is correct:
If both @DynamoDbAutoGeneratedUuid and @DynamoDbAutoGeneratedKey are applied to the same field, both extensions will execute in chain order. Since AutoGeneratedKeyExtension checks for existing values before generating, the behavior depends on registration order:
- If
AutoGeneratedUuidExtension(current) runs first: It generates a UUID, thenAutoGeneratedKeyExtension(new) sees the value exists and skips generation - If
AutoGeneratedKeyExtension(current) runs first: It generates conditionally, thenAutoGeneratedUuidExtension(old) overwrites it anyway
This creates unpredictable behavior. If this is the case, can we please add some tests that cover it, and throw an exception when both annotations (new and current) are applied to the same field?
- Fix documentation for WRITE_IF_NOT_EXISTS:
In v1, @DynamoDBAutoGeneratedKey used DynamoDBAutoGenerateStrategy.CREATE, which only generated UUIDs when the annotated value was null.
The v2 Enhanced Client’s @DynamoDbAutoGeneratedUuid removed this conditional logic entirely - it always generates regardless of existing values. The javadoc documents this as intentional:
"Every time a record with this attribute is written to the database it will update the attribute with a UUID#randomUUID string."
but also, misleadingly suggests using UpdateBehavior.WRITE_IF_NOT_EXISTS as a workaround, which doesn’t work for primary keys due to DynamoDB’s updateItem API not allowing conditional updates on primary key attributes.
We will likely ask you to add more test coverage similar to other extensions test coverage, but I will provide more concrete about testing gaps in the near future after another review.
Thanks,
Ran~
Uh oh!
There was an error while loading. Please reload this page.
Description
Added the facility of using an annotation that will auto-generate a key for an attribute in a class,
similar to the legacy V1
@DynamoDBAutoGeneratedKey, now ported and adapted for V2 with the Enhanced Client.Important Restriction
This annotation is only valid for primary keys (PK/SK) or secondary index keys (GSI/LSI PK/SK).
If applied to a non-key attribute, the extension will throw an
IllegalArgumentException.If the attribute is not provided by the client, the SDK will automatically generate a value (UUID by default)
during serialization. If the attribute already has a value, it will be preserved depending on the update policy applied.
The implementation reuses the concept from V1 but leverages the V2 Enhanced Client extension model to achieve parity.
A key aspect of this feature is its combination with
@DynamoDbUpdateBehaviorfor secondary index keys only. This determines whether a generated key is one-time only or is regenerated on every update:WRITE_ALWAYS) → The attribute will be regenerated on every write if missing, even during updates. Useful for attributes likelastUpdatedKeythat are meant to refresh often.WRITE_IF_NOT_EXISTS→ The attribute will only be generated once (on the first insert) and preserved across updates. This is the recommended option for stable identifiers likecreatedKey.Motivation and Context
#5497
This functionality was present in V1 under
@DynamoDBAutoGeneratedKey. Many users requested its reintroduction in V2.By introducing this annotation and the supporting extension, we align the Enhanced Client API with developer expectations and provide a familiar migration path.
Modifications
@DynamoDbAutoGeneratedKeyin thesoftware.amazon.awssdk.enhanced.dynamodb.extensions.annotationspackage.AutoGeneratedKeyExtension, which ensures attributes annotated with@DynamoDbAutoGeneratedKeyare populated with a UUID when absent. This usesUUID.randomUUID()under the hood.AutoGeneratedKeyTagas the annotation tag integration point.@DynamoDbUpdateBehaviorto support both stable one-time keys - works for secondary index keys only (WRITE_IF_NOT_EXISTS) and regenerating keys (WRITE_ALWAYS).Stringis supported).AutoGeneratedKeyExtensionTest) and functional integration tests (AutoGeneratedKeyRecordTest) to verify correct behavior with real DynamoDB operations.and integration with
VersionedRecordExtension.Testing
The changes have already been tested by running the existing tests and also added new unit/integration tests
for the new flow, ensuring parity with V1 behavior while also validating V2-specific integration points.
Test Coverage Checklist
Types of changes
Checklist
mvn installsucceedsscripts/new-changescript and following the instructions. Commit the new file created by the script in.changes/next-releasewith your changes.License